package top.hidoctor.auscultation.net.p;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;

import com.tang.base.BaseResponse;
import com.tang.base.NetMapEncryptUtils;
import com.tang.net.SuccessConsumer;
import com.tang.user.UserInfoManager;

import java.util.Map;

import io.reactivex.functions.Consumer;
import top.hidoctor.auscultation.bean.AuditListBean;
import top.hidoctor.auscultation.net.PresenterWrapper;
import top.hidoctor.auscultation.net.c.AuditListContract;
import top.hidoctor.auscultation.net.req.AuditReq;

public class AuditListPresenter extends PresenterWrapper<AuditListContract.View> implements AuditListContract.Presenter {


    public AuditListPresenter(Context context, AuditListContract.View view) {
        super(context, view);
    }

    @Override
    public void getList(AuditReq req, SwipeRefreshLayout refreshLayout, boolean isUser) {
        if (refreshLayout != null && !refreshLayout.isRefreshing()) {
            mView.showLoading();
        }
        Map<String, Object> map = new NetMapEncryptUtils(req).getMaps();
        if (isUser) map.put("userId", UserInfoManager.getInstance().getUserInfo().getUserId());
        add(mService.auditList(map)
                .compose(this.<AuditListBean>getTransformer())
                .subscribe(new SuccessConsumer<BaseResponse<AuditListBean>>(mView, mContext) {
                    @Override
                    public void onSuccess(BaseResponse<AuditListBean> response) {
                        mView.hideLoading();
                        mView.getListSuc(response.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                }));
    }

    @Override
    public void deleteAudit(String id) {
        mView.showLoading();
        Map<String, Object> map = new NetMapEncryptUtils("").getMaps();
        add(mService.deleteAudit(id, map)
                .compose(this.getTransformer())
                .subscribe(new SuccessConsumer<BaseResponse<Object>>(mView, mContext) {
                    @Override
                    public void onSuccess(BaseResponse<Object> response) {
                        mView.hideLoading();
                        mView.deleteAuditSuc();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                }));
    }

}
